package com.tianlaiedu.controller;

import com.alibaba.fastjson.JSON;
import com.tianlaiedu.bean.Comment;
import com.tianlaiedu.bean.ResultVO;
import com.tianlaiedu.service.impl.CommentServiceImpl;
import com.tilaiedu.framework.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:hejing
 * @date:2021/12/3
 */
public class CommentAction {
    private static final Logger LOGGER = LoggerFactory.getLogger(Login.class);

    @RequestMapping("/comment/add")
    public void addcomment(HttpServletRequest request, HttpServletResponse response)throws IOException, ServerException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        ResultVO resultVO = new ResultVO();
        resultVO.setType(-1);
        // 通过json功能类来获取json数据
        Map<String, Object> res = JSON.parseObject(request.getInputStream(), HashMap.class);
        LOGGER.debug("=>前端传入的数据:" + res);
        System.out.println("res = " + res);
        Comment comment = new Comment();
        CommentServiceImpl commentService = new CommentServiceImpl();
        comment.setPost_id((Integer) res.get("postid"));
        comment.setUser_id((Integer) res.get("userid"));
        comment.setComment((String) res.get("ccomment"));
        int addCom = commentService.addCom(comment);
        if (addCom>0){
            resultVO.setType(0);
            response.getWriter().write(JSON.toJSONString(resultVO));
        }else {
            response.getWriter().write(JSON.toJSONString(resultVO));
        }
    }
    @RequestMapping("/comment/find")
    public void findcomment(HttpServletRequest request,HttpServletResponse response)throws IOException,ServerException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        ResultVO resultVO = new ResultVO();
        resultVO.setType(-1);
        Map<String, Object> res = JSON.parseObject(request.getInputStream(), HashMap.class);
        LOGGER.debug("=>前端传入的数据:" + res);
        System.out.println("res = " + res);
        System.out.println(res.get("id"));
        if (res.get("id")==null){
            return;
        }
        List<Comment> commentList = new CommentServiceImpl().finCom((Integer)res.get("id"));
        System.out.println("commentList = " + commentList);
        resultVO.setType(0);
        resultVO.setData(commentList);
        response.getWriter().write(JSON.toJSONString(resultVO));
    }
}
